from django.utils.decorators import method_decorator

from gis.common.django_ext.decorators import validate_parameters
from gis.common.django_ext.forms import PaginationSchema
from gis.common.django_ext.views import BaseView, PaginationResponse
from itpay.core.agency import quick_reply_service
from itpay.server_agency.decorators import require_login
from . import quick_reply_form
from .helper import wrapper_name


class QuickReplyListViewListView(BaseView):
    @method_decorator(require_login())
    @method_decorator(validate_parameters(PaginationSchema))
    def get(self, request, cleaned_data):
        agency_id = request.user_id
        total, quick_reply_list = quick_reply_service.list_quick_reply(**cleaned_data, agency_id=agency_id)
        return PaginationResponse(total, quick_reply_list)

    @method_decorator(require_login())
    @method_decorator(validate_parameters(quick_reply_form.AddQuickReplySchema))
    def post(self, request, cleaned_data):
        agency_id = request.user_id
        return quick_reply_service.add_quick_reply(agency_id=agency_id, **cleaned_data)


class QuickReplyDetailView(BaseView):
    @method_decorator(require_login())
    def delete(self, request, quick_reply_id):
        quick_reply_service.delete_quick_reply(quick_reply_id)
        return dict(id=quick_reply_id)

    @method_decorator(require_login())
    @method_decorator(validate_parameters(quick_reply_form.UpdateQuickReplySchema))
    def put(self, request, quick_reply_id, cleaned_data):
        quick_reply_service.update_quick_reply(quick_reply_id, **cleaned_data)
        return dict(id=quick_reply_id)
